package com.kasinf.sample.datapermission.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.kasinf.framework.common.pojo.LoginUser;
import com.kasinf.framework.satoken.utils.LoginHelper;
import com.kasinf.sample.datapermission.controller.vo.LoginParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author : Kaiser
 * @Date: 2025/5/16  10:43
 */
@RestController
@RequestMapping("login")
public class LoginController {


    @PostMapping("basic")
    public String login(@RequestBody LoginParam param) {
        // 1. 通过用户名，查询数据库获得用户信息
        if (!param.getUsername().equals("admin")) {
            throw new RuntimeException("用户名或密码错误");
        }
        // 2. 校验用户密码
        if (!param.getPassword().equals("123456")) {
            throw new RuntimeException("用户名或密码错误");
        }
        // 3. 构造登录用户实体
        LoginUser loginUser = new LoginUser();
        loginUser.setId(1L);
        loginUser.setUsername(param.getUsername());
        loginUser.setDeptCode("A01");
        loginUser.setDeptId(1L);
        loginUser.setUserType(0);
        loginUser.setTenantId(1L);
        // 4. 调用sa-token的登录
        LoginHelper.login(loginUser, null);
        // 5. 获取当前token，返回给前端
        return StpUtil.getTokenValue();
    }
}
